import 'package:flutter/material.dart';

// AnimationController 是一个特殊的 Animation 对象，它可以生成新的动画值。它通常与 Tween 一起使用，以生成从一个值到另一个值的动画。
// 它提供了控制动画的各种方法和属性

// 淡入淡出动画实现
class AmiCtrl extends StatefulWidget {
  const AmiCtrl({super.key});

  @override
  State<AmiCtrl> createState() => _AmiCtrlState();
}

class _AmiCtrlState extends State<AmiCtrl> with TickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<double> _animation;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _controller = AnimationController(vsync: this, duration: const Duration(seconds: 2));

    _animation = Tween<double>(begin: 0.0, end: 1.0).animate(_controller)
      ..addListener(() {
        setState(() {
          
        });
      });

    _controller.forward();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      height: 400,
      decoration: BoxDecoration(
        border: Border.all(color: Colors.black, width: 1),
        borderRadius: BorderRadius.circular(6)
      ),
      child: Column(
        children: [
          ElevatedButton(
            onPressed: () {
              if (_controller.isCompleted) {
                _controller.reverse();
              } else {
                _controller.forward();
              }
            },
            child: const Text('切换 AnimationController')
          ),

          Center(
            child: Column(
              children: [
                Opacity(
                  opacity: _animation.value,
                  child: Container(
                    width: 200,
                    height: 200,
                    color: Colors.amber[200],
                  ),
                ),

                Container(
                  width: 200,
                  height: 100,
                  color: const Color.fromARGB(255, 171, 155, 107),
                )
              ],
            )
          )
        ],
      ),
    );
  }
}